package net.tensor.nfs.client.utils;

import com.emc.ecs.nfsclient.nfs.io.Nfs3File;
import com.emc.ecs.nfsclient.nfs.io.NfsFileInputStream;
import com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream;
import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3;
import com.emc.ecs.nfsclient.rpc.CredentialUnix;

import java.io.*;

/**
 * @author Q
 */
public class NfsTransferFile {

    private static final String NFS_IP = "192.168.152.135";
    private static final String NFS_DIR = "/root/nfs_root";

    public static void main(String[] args) {
//        uploadFileToNfs();
        downLoadFileFromNfs();
    }

    //上传本地文件到Nfs服务器指定目录
    public static void uploadFileToNfs() {
        String localDir = "C:\\Users\\Q\\Desktop\\huitian-bpm-k8s\\yaml\\web\\admin-web.yml";
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            //创建一个本地文件对象
            File localFile = new File(localDir);
            //获取本地文件的文件名，此名字用于在远程的Nfs服务器上指定目录创建同名文件
            String localFileName = localFile.getName();
            Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3);
            //创建远程服务器上Nfs文件对象
            Nfs3File nfsFile = new Nfs3File(nfs3, "/" + localFileName);
            //打开一个文件输入流
            inputStream = new BufferedInputStream(new FileInputStream(localFile));
            //打开一个远程Nfs文件输出流，将文件复制到的目的地
            outputStream = new BufferedOutputStream(new NfsFileOutputStream(nfsFile));

            //缓冲内存
            byte[] buffer = new byte[1024];
            while ((inputStream.read(buffer)) != -1) {
                outputStream.write(buffer);
            }
            System.out.println("文件上传完成！");
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    //从Nfs服务器上下载指定的文件到本地目录
    public static void downLoadFileFromNfs() {
        String nfsFileDir = "admin-web.yml";
        String localDir = "C:\\Users\\Q\\Downloads";
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3);
            //创建远程服务器上Nfs文件对象
            Nfs3File nfsFile = new Nfs3File(nfs3, nfsFileDir);
            String localFileName = localDir + nfsFile.getName();
            //创建一个本地文件对象
            File localFile = new File(localFileName);
            //打开一个文件输入流
            inputStream = new BufferedInputStream(new NfsFileInputStream(nfsFile));
            //打开一个远程Nfs文件输出流，将文件复制到的目的地
            outputStream = new BufferedOutputStream(new FileOutputStream(localFile));

            //缓冲内存
            byte[] buffer = new byte[1024];

            while (inputStream.read(buffer) != -1) {
                outputStream.write(buffer);
            }
            System.out.println("文件下载完成！");
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

